﻿Imports System.Data.SqlClient
Imports RunEnv
Public Class dlgUser
    Private mblnIsAddNewMode As Boolean
    Private mblnIsOK As Boolean
    Private mobjUpdateCommand As SqlCommand
    Private mobjInsertCommand As SqlCommand

    Public Function ShowOpen(Optional ByVal userID As String = "", Optional ByVal userName As String = "", Optional ByVal isStop As Boolean = True) As Boolean
        mblnIsOK = False
        If Len(userID) = 0 Then
            mblnIsAddNewMode = True
        Else
            mblnIsAddNewMode = False
        End If

        Me.txtUserID.Text = userID
        Me.txtUserName.Text = userName
        Me.chkIsStop.Checked = isStop

        InitUI()
        InitDB()

        Me.ShowDialog()
        Return mblnIsOK
    End Function

    Private Sub InitUI()
        Me.txtUserID.Enabled = mblnIsAddNewMode
        Me.txtUserName.Enabled = mblnIsAddNewMode
        Me.cmdAdd.Enabled = mblnIsAddNewMode

        If mblnIsAddNewMode Then
            Me.Text = "新增用户"
        Else
            Me.Text = "修改用户"
        End If
    End Sub


    Private Sub InitDB()
        mobjInsertCommand = New SqlCommand
        mobjInsertCommand.Connection = CRunEnv.DBConnection.sqlConnection
        mobjInsertCommand.CommandText = "Insert Into B_User (UserID,UserName,IsStop) Values " _
                & "(@UserID,@UserName,@IsStop)"
        mobjInsertCommand.Parameters.Add("@UserID", SqlDbType.NVarChar)
        mobjInsertCommand.Parameters.Add("@UserName", SqlDbType.NVarChar)
        mobjInsertCommand.Parameters.Add("@IsStop", SqlDbType.Bit)

        mobjUpdateCommand = New SqlCommand
        mobjUpdateCommand.Connection = CRunEnv.DBConnection.sqlConnection
        mobjUpdateCommand.CommandText = "Update B_User Set IsStop = @IsStop" _
                                & " Where UserID=@UserID"
        mobjUpdateCommand.Parameters.Add("@IsStop", SqlDbType.Bit)
        mobjUpdateCommand.Parameters.Add("@UserID", SqlDbType.NVarChar)
    End Sub

    Private Sub cmdCancel_Click(sender As System.Object, e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub

    Private Sub cmdAdd_Click(sender As System.Object, e As System.EventArgs) Handles cmdAdd.Click
        If Not CheckData() Then Exit Sub
        If EditSave() Then
            Me.txtUserID.Text = ""
            Me.txtUserName.Text = ""
            Me.chkIsStop.Checked = False
            mblnIsOK = True
        End If
    End Sub

    Private Sub cmdOK_Click(sender As System.Object, e As System.EventArgs) Handles cmdOK.Click
        If Not CheckData() Then Exit Sub
        If EditSave() Then
            mblnIsOK = True
            Me.Close()
        End If
    End Sub

    Private Function CheckData() As Boolean
        txtUserID.Text = Trim(txtUserID.Text)
        txtUserName.Text = Trim(txtUserName.Text)
        If Len(txtUserID.Text) <= 0 Then
            MsgBox("登陆名不能为空！", MsgBoxStyle.Exclamation, "系统提示")
            Return False
        End If

        If Len(txtUserName.Text) <= 0 Then
            MsgBox("用户名不能为空", MsgBoxStyle.Exclamation, "系统提示")
            Return False
        End If

        Dim sql As String
        sql = "Select Count(*) From B_User" _
            & " Where UserID ='" & txtUserID.Text & "'"
        If CRunEnv.DBConnection.GetIntCalculateValue(sql) <> 0 Then
            MsgBox("登陆名已存在！", MsgBoxStyle.Exclamation, "系统提示")
            Return False
        End If
        Return True
    End Function

    Private Function EditSave() As Boolean
        If mblnIsAddNewMode Then
            Dim trans As SqlTransaction
            Dim command As SqlCommand
            Dim sql As String
            trans = CRunEnv.DBConnection.sqlConnection.BeginTransaction()
            Try
                sql = "insert into B_UserRight (UserID,RightID,RightValue,IsAllowMana )" _
                    & " select '" & Me.txtUserID.Text & "' As UserID,B_URSysFunc.RightID, 0 as RightValue, 0 As IsAllowMana" _
                    & " from B_URSysFunc "
                command = New SqlCommand(sql, CRunEnv.DBConnection.sqlConnection)
                command.Transaction = trans
                command.ExecuteNonQuery()

                mobjInsertCommand.Transaction = trans
                mobjInsertCommand.Parameters("@UserID").Value = Me.txtUserID.Text
                mobjInsertCommand.Parameters("@UserName").Value = Me.txtUserName.Text
                mobjInsertCommand.Parameters("@IsStop").Value = Me.chkIsStop.Checked
                mobjInsertCommand.ExecuteNonQuery()

                trans.Commit()
                MsgBox("新增用户成功。", MsgBoxStyle.Information, "系统提示")
                Return True
            Catch ex As Exception
                MsgBox("保存失败，" & ex.Message, MsgBoxStyle.Exclamation, "系统提示")
                trans.Rollback()
                Return False
            End Try
        Else
            Try
                mobjUpdateCommand.Parameters("@IsStop").Value = Me.chkIsStop.Checked
                mobjUpdateCommand.Parameters("@UserID").Value = Me.txtUserID.Text
                If mobjUpdateCommand.ExecuteNonQuery() = 1 Then
                    MsgBox("修改用户成功", MsgBoxStyle.Information, "系统提示")
                    Return True
                End If
            Catch ex As Exception
                MsgBox("保存失败，" & ex.Message, MsgBoxStyle.Exclamation, "系统提示")
                Return False
            End Try
        End If
        Return True
    End Function
End Class